package com.hongsheng.controller;/*
@author:刘逸思普
@Date:2023/8/9
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import com.hongsheng.pojo.*;
import com.hongsheng.service.*;
import com.hongsheng.service.impl.CustomerServiceImpl;
import com.hongsheng.util.Re;
import com.hongsheng.util.ReqBody;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Queue;

@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private DriverService driverService;
    @Autowired
    private DepositoryService depositoryService;
    @Autowired
    private LineService lineService;
//    @GetMapping
//    public R findOrderList(@PathVariable Integer currentPage,@PathVariable Integer pageSize,@RequestBody Order order){
////        IPage<Order> iPage= orderService.getPage(currentPage,pageSize,order);
//        return new R(true,null);
//    }
    @GetMapping
    public R findOrderList(){
        orderService.driverStu();
        List<Order> orders = orderService.selectOrders();
        return new R(true,orders);
    }
    @GetMapping("/customer")
    public R findCustomerList(){
        List<Customer> customers = customerService.list(null);
        return new R(true,customers);
    }
    @PostMapping("/driver")
    public R findFreeDriverList(@RequestBody Order order){
        List<Driver> drivers = orderService.findDrivers(order);
        return new R(true,drivers);
    }
    @GetMapping("/depository")
    public R findDepositoryList(){
        List<Depository>  depositoryList= depositoryService.findAllDepository();
        return new R(true,depositoryList);
    }
    @GetMapping("/startAddress")
    public R findStartAddressList(){
        List<String>  startAddress= orderService.findStartAddress();
        return new R(true,startAddress);
    }
    @GetMapping("/{id}")
    public R findOrderById(@PathVariable Integer id){
       Order order= orderService.findOrderById(id);
       orderService.setAmount(order);
        return new R(true,order);
    }
    @PostMapping("/page")
     public R findOrderListByLimit(@RequestBody ReqBody reqBody){
        orderService.driverStu();
        PageInfo<Order> orderPageInfo = orderService.selectOrdersByLimit(reqBody.getCurrentPage(), reqBody.getPageSize(), reqBody.getOrder());
        return new R(true,orderPageInfo);
    }
    @PutMapping
    public R updateOrder(@RequestBody Order order){
        orderService.driverStu();
        String msg;
        if (order.getOrderStatus()>=3){
            msg="订单已发货无法修改！";
            return new R(false,msg);
        }else {
            Boolean flag = orderService.updateOrder(order);
            if (flag){
                msg="修改成功!";
            }else {
                msg="修改遇到了一点问题，请重试";
            }
            return new R(flag,msg);
        }

    }
    @PutMapping("/send")
    public R updateStatus(@RequestBody Order order){
        Order orderById = orderService.findOrderById(order.getId());

        if (orderById.getGoodsList()==null){
            return new R(false,"订单尚未添加货物，无法发货");
        }
        orderService.driverStu();
        String msg;
        if (orderById.getOrderStatus()>=3){
            msg="发货失败,请检查订单是否已经发货，或联系管理员处理";
            return new R(false,msg);
        }else {
            for (Driver driver : orderById.getDrivers()) {
                driver.setStatus(2);
                driver.setLine(order.getLine());
                driver.setAddress(order.getLine().getStartAddress());
                driverService.updateDriver(driver);
                driverService.setStuDr(driver);
            }
            orderById.setOrderStatus(3);
            orderById.setSendTime(new Date());

            Boolean flag = orderService.updateOrder(orderById);
            Boolean billOut = orderService.getBillOut(orderById);
           Boolean billIn= orderService.billIn(orderById);
            if (flag&&billOut&&billIn){
                msg="发货成功";
            }else {
                msg="发货失败,请检查订单，或联系管理员处理";
            }
            return new R(flag,msg);
        }
    }
    @PutMapping("/end")
    public R endOrder(@RequestBody Order order){
        orderService.driverStu();
        if (order.getOrderStatus()==3){
            order.setOrderStatus(4);
            Boolean flag = orderService.updateOrder(order);
            for (Driver driver : order.getDrivers()) {
                System.out.println(order.getLine().getEndAddress());
                driver.setStatus(0);
                driver.setAddress(order.getLine().getEndAddress());
                driverService.updateDriver(driver);
            }
            String msg;
            if (flag){
                msg="收货成功";
            }else {
                msg="收货失败，请检查订单或联系管理员";
            }
            return new R(flag,msg);
        }else if (order.getOrderStatus()==4){
            return new R(false,"订单已到货，请勿重复操作");
        }else {
            return new R(false,"订单尚未发货");
        }
    }
    @DeleteMapping("/{id}")
    public R deleteOrder(@PathVariable Integer id){
        Boolean aBoolean = orderService.deleteOrder(id);
        String msg="";
        if (aBoolean){
            msg="删除成功";
        }else{
            msg="删除失败";
        }
        return new R(aBoolean,msg);
    }

    @PostMapping("/goods")
    public R insert(@RequestBody Goods goods){
        Driver driver = goods.getDriver();
        driver.setStatus(1);
        Order orderById = orderService.findOrderById(goods.getOrder_id());
        Line line = orderById.getLine();
        driver.setLine(line);
        driver.setAddress(line.getStartAddress());
        Boolean aBoolean = driverService.updateDriver(driver);
        Boolean flag = goodsService.insertGoods(goods);
        Boolean aBoolean1 = orderService.setAmount(orderById);
        return new R(flag&&aBoolean&&aBoolean1);
    }
    @PostMapping
    public R addOrder(@RequestBody Order order){
        Boolean flag = orderService.addOrder(order);
        System.out.println(order);
        String msg="";
        if (flag){
            msg="添加成功";
        }else {
            msg="添加失败";
        }
        return new R(flag,msg);

    }


    @GetMapping("/depository/{id}")
    public R getCapacity(@PathVariable Integer id){
        Double capacity = orderService.findCapacity(id);
        if (capacity==null){
            capacity=0.0;
        }
        return new R(true,capacity);
    }
    @GetMapping("/totalSize/{id}")
    public R getTotalSize(@PathVariable Integer id){
        Double totalSize = orderService.totalSize(id);
        if (totalSize==null){
            totalSize=0.0;
        }
        return new R(true,totalSize);
    }
    @GetMapping("/totalWeight/{id}")
    public R getTotalWeight(@PathVariable Integer id){
        Double totalWeight = orderService.totalWeight(id);
        if (totalWeight==null){
            totalWeight=0.0;
        }
        return new R(true,totalWeight);
    }

    /**
     * 查询月份订单数量
     * @return
     */
    @GetMapping("/selectOrderByMonth")
    public R selectOrderByMonth(){
        return new R(true,orderService.selectOrderByMonth());
    }


    @GetMapping("/home")
    public Re selectHome(){
      Integer order=orderService.getOrderCount();
      Double billIn=orderService.getBillIn();
      Double billOut=orderService.getBillOut();
      Integer customer=orderService.getCustomer();
      Re re=new Re(order,billIn,billOut,customer);
      return re;
    }
}
